package com.c.ctgproject.ui.storage.material

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.storage.StorageMaterialInAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.storage.StorageMaterialInListEntity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_storage_material_in.*
import java.util.ArrayList

/**
 *作者:LiBW
 *创建日期: 2020/9/14
 *描述:仓储 物料入库确认页面
 */
class StorageMaterialInActivity : BaseActivity(), StorageMaterialInAdapter.Callback {
    private var page = 1
    private var size = 5
    private val mViewModel: StorageMaterialInViewModel by lazy { ViewModelProvider(this)[StorageMaterialInViewModel::class.java] }
    private val mList = ArrayList<StorageMaterialInListEntity.DataBean.ListBean>()
    private val mAdapter: StorageMaterialInAdapter by lazy { StorageMaterialInAdapter(this, mList, this) }
    override fun getLayout() = R.layout.activity_storage_material_in

    override fun event() {
        changeTransparentStatusBar()
        mStorageRV.adapter = mAdapter
        mStorageRV.layoutManager = LinearLayoutManager(this)

        mViewModel.detail.observe(this, Observer {
            mStorageRefresh.over()
            loading.dismiss()
            MyLog.d("物料入库列表回调:$it")
            it.handlerCall({
                val entity = getGson().fromJson(it, StorageMaterialInListEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }, { error ->
                showToast(error)
            })
        })
        mViewModel.confirmResult.observe(this, Observer {
            mStorageRefresh.over()
            loading.dismiss()
            MyLog.d("物料入库确认回调:$it")
            it.handlerCall({
                showToast("物料入库成功")
                initData()
            }, { error ->
                showToast(error)
            })
        })
        mViewModel.cancelResult.observe(this, Observer {
            mStorageRefresh.over()
            loading.dismiss()
            MyLog.d("物料入库取消回调:$it")
            it.handlerCall({
                showToast("物料取消入库成功")
                initData()
            }, { error ->
                showToast(error)
            })
        })
        mViewModel.error.observe(this, Observer {
            mStorageRefresh.over()
            MyLog.d("物料入库确认失败:$it")
            loading.dismiss()
            showToast(it)
        })
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    fun initData() {
        loading.show()
        mList.clear()
        page = 1
        mViewModel.getList(page, size)
    }

    override fun click() {
        mStorageRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getList(page, size)
        }
        //下拉刷新
        mStorageRefresh.setOnRefreshListener {
            initData()
        }
    }

    override fun confirm(materialInNo: String, inWeight: Double, materialId: Int, inNum: Int) {
        commonDialog("确认物料入库么"){
            mViewModel.confirm(1, materialInNo, inWeight, materialId, inNum)
        }
    }

    override fun cancel(materialInNo: String) {
       commonDialog("确认取消物料入库么"){
           mViewModel.cancel(materialInNo)
       }
    }
}